package java6703.lesson02.check;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.*;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
//import java.awt.*;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CheckRegex6703 extends Application {

    int i =0;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        Scene scene = new Scene(getRoot(), 1000, 700);
        primaryStage.setScene(scene);
        primaryStage.setTitle("验证正则表达式");
        primaryStage.show();
    }

    private Parent getRoot() {

        //Top
        Label lblStudent = new Label("212106703 陈杰");
        //【注意】多行代码的书写规范
        BorderStroke bs = new BorderStroke(Paint.valueOf("#8FBC8F")
                , BorderStrokeStyle.DOTTED
                , new CornerRadii(10)
                , new BorderWidths(3));
        lblStudent.setBorder(new Border(bs));
        lblStudent.setPadding(new Insets(10));

        //Left
        VBox vRadio = new VBox(30);
        RadioButton rb1 = new RadioButton("第一题");
        RadioButton rb2 = new RadioButton("第二题");
        RadioButton rb3 = new RadioButton("第三题");
        RadioButton rb4 = new RadioButton("第四题");
        ToggleGroup tg = new ToggleGroup();
        //RadioButton要用ToggleGroup分组（逻辑分组）
        rb1.setToggleGroup(tg);
        rb2.setToggleGroup(tg);
        rb3.setToggleGroup(tg);
        rb4.setToggleGroup(tg);
        vRadio.getChildren().addAll(rb1, rb2, rb3, rb4);
        vRadio.setAlignment(Pos.CENTER);
        vRadio.setPadding(new Insets(10));

        //Center
        TextArea taTitle = new TextArea("String str = \"2816001张达2018年第1学期成绩：数学83.2分，\"+\n" +
                "    \"英语91.7分，语文88分，作文80分。\";\n" +
                "String regex =\" 填空 1 \";\n" +
                "Pattern p = 填空 2 ;\n" +
                "Matcher m = 填空 3 ;\n" +
                "double sum = 0;\n" +
                "while (m.find()) {\n" +
                "  sum += Double.parseDouble( str.substring( 填空 4 , 填空 5 ) );\n" +
                "}\n" +
                "System.out.println(\"sum=\" + sum);");
        taTitle.setEditable(true);

        HBox hInput = new HBox(10);
        Label lblInput = new Label("输入：");
        TextField tfInput = new TextField("");
        tfInput.setPromptText("输入被分析的字符串");
        hInput.getChildren().addAll(lblInput, tfInput);
        hInput.setAlignment(Pos.CENTER_LEFT);

        HBox hRegex = new HBox(10);
        Label lblRegex = new Label("正则表达式：");
        TextField tfRegex = new TextField();
        hRegex.getChildren().addAll(lblRegex, tfRegex);
        hRegex.setAlignment(Pos.CENTER_LEFT);

        Button button = new Button("检验");

        VBox vCenter = new VBox(20);
        vCenter.getChildren().addAll(taTitle, hInput, hRegex, (Node) button);
        vCenter.setPadding(new Insets(20.0));
        vCenter.setAlignment(Pos.TOP_CENTER);
        VBox.setVgrow(taTitle, Priority.ALWAYS);
        HBox.setHgrow(tfInput, Priority.ALWAYS);
        HBox.setHgrow(tfRegex, Priority.ALWAYS);

        //Bottom
        Label lblPrint = new Label();

        BorderPane root = new BorderPane();
        root.setTop(lblStudent);
        root.setLeft(vRadio);
        root.setBottom(lblPrint);
        root.setCenter(vCenter);
        root.setPadding(new Insets(10));
        BorderPane.setAlignment(lblStudent, Pos.CENTER);
        BorderPane.setAlignment(lblPrint, Pos.CENTER);

        root.setStyle("-fx-font-size:20");//用CSS样式设置容器中所有节点的字体大小
        rb1.setSelected(true);


        /**
         * （1）在左边的一组单选按钮中选择题目;
         * （2）在多行文本框中显示题目的具体内容;
         * （3）在单行文本框中输入本题要处理的字符串和正则表达式;
         * （4）单击检验按钮，显示检验结果。
         */

        //遍历数组，为每个题目添加相应题目
        List<String> list = new ArrayList<>();
        list.add("String str = \"2816001张达2018年第1学期成绩：数学83.2分，\"+\n" +
                "    \"英语91.7分，语文88分，作文80分。\";\n" +
                "String regex =\" 填空 1 \";\n" +
                "Pattern p = 填空 2 ;\n" +
                "Matcher m = 填空 3 ;\n" +
                "double sum = 0;\n" +
                "while (m.find()) {\n" +
                "  sum += Double.parseDouble( str.substring( 填空 4 , 填空 5 ) );\n" +
                "}\n" +
                "System.out.println(\"sum=\" + sum);");


        list.add("String str = \"E:\\\\JavaTest\\\\02LessonXXXX\\\\src\";\n" +
                "String regex =\" 填空 1 \";\n" +
                "String[] dirs = str.split(regex);\n" +
                "System.out.println( 填空 2 );");


        list.add("String str = \"第1节String第2节Object第3节Collection\";\n" +
                "String regex = \"[a-zA-Z] 填空 2\";\n" +
                "System.out.println(str.填空 3 );");


        list.add("String str = ”2018年成绩：数学83分，英语91分，语文88分，作文80分。“\n" +
                "String regex=【1】;\n" +
                "String[] words=str.split(regex);\n" +
                "System.out.print(  【2】 )");

        List<String> answer = new ArrayList<>();
        answer.add("数学83.2分， 英语91.7分，语文88分，作文80分");
        answer.add("E:,JavaTest,02LessonXXXX,src");
        answer.add("String***Object***Collection***");
        answer.add("[2018,83,91,88,80]");


        //为题目1赋值题目，创建点击监听事件
        rb1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                taTitle.setText(list.get(0));
            }
        });

        //为题目2赋值题目，创建点击监听事件
        rb2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                taTitle.setText(list.get(1));
            }
        });

        //为题目3赋值题目，创建点击监听事件
        rb3.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                taTitle.setText(list.get(2));
            }
        });

        //为题目4赋值题目，创建点击监听事件
        rb4.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                taTitle.setText(list.get(3));
            }
        });

        //为答案创建点击监听事件
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                //输出答案
                if (i == 0){
                    lblPrint.setText(answer.get(i));
                    i++;
                }else if (i==1){
                    lblPrint.setText(answer.get(1));
                    i++;
                }else if (i==2){
                    lblPrint.setText(answer.get(2));
                    i++;
                }else if (i==3){
                    lblPrint.setText(answer.get(3));
                }

//                lblPrint.setText(answer.get(0));

            }
        });

        return root;
    }
}
